/**
 * This class is an session bean class
 * that performes all the oprations for 
 * the collaborateur entity.
 * It implements the IPerformeCollaborator interface
 */
package com.teamreinforce.ejb.model;

import com.teamreinforce.ejb.entities.Collaborateur;
import com.teamreinforce.ejb.entities.Projet;
import com.teamreinforce.ejb.entities.Tache;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author tidiane
 */
@Stateless
public class PerformeCollaborator implements IPerformeCollaborator {

    @PersistenceContext( unitName= "TeamreinforceEJBPU")
    private EntityManager em;
    
    @Override
    public void createCollaborateur(String prenom, String nom,String telephone,String portable, String email, String login,String password, boolean est_admin ) {
        // Verification si le login n'existe pas dejas !!
        if ( getCollaboratorByLogin(login) != null){
            throw new EJBException("Un collaborateur exist deja avec le login : " + login);
        }
        Collaborateur _collaborateur = new Collaborateur(Integer.MIN_VALUE, nom, prenom, telephone, portable, email, login, password);
        _collaborateur.setLogin(login);
        _collaborateur.setPassword(password);
        if( est_admin != true) _collaborateur.setEstAdmin(0);
        else if(est_admin == true) _collaborateur.setEstAdmin(1);
        em.persist(_collaborateur);
    }

    @Override
    public Collaborateur getCollaborator(String login, String password) {
        Collaborateur collaborator = null;
        try{
            TypedQuery<Collaborateur> query = em.createQuery("SELECT c FROM Collaborateur c WHERE c.login =:login AND c.password =:password", Collaborateur.class);
            //Query query = em.createQuery("SELECT c FROM Collaborateur c WHERE c.login =:login AND c.password =:password");
            query.setParameter("login", login);
            query.setParameter("password", password);
            collaborator = (Collaborateur)query.getSingleResult();
        }
        catch(EJBException e){
            System.err.println("EBJException in getCollaborator method");
            return null;
        }
        
        return collaborator;
    }
    
    @Override
    public List<Tache> getCollaboratorTasks(Projet project, Collaborateur collaborator) {
        List<Tache> tasks = new ArrayList<Tache>();
        for ( Tache task : collaborator.getTacheList()){
            Projet _project = task.getEtape().getProjet();
            if ( _project.equals(project))
                tasks.add(task);
        }
        return tasks;
    }

    @Override
    public Collaborateur getCollaboratorByLogin(String login) {
        TypedQuery<Collaborateur> query = em.createNamedQuery("Collaborateur.findByLogin", Collaborateur.class);
        query.setParameter("login", login);
        try {
            return query.getSingleResult();
        }
        catch (Exception e){
            return null;
        }
    }

    @Override
    public List<Collaborateur> getAllCollaborators() {
        return em.createQuery("SELECT c FROM Collaborateur c", Collaborateur.class).getResultList();
    }
}
